﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace RefOffPDA
{
    /*
     * Class: Home
     * 
     * Use: The main screen of the application, allowing the
     *      user to start a new match or reset data from previous
     *      matches
     *      
     * Author: Mike Skaife, mcs3@aber.ac.uk
     */ 
    public partial class Home : Form
    {  
        //Details used to connect to the database
        string conString;
        SqlCeConnection conn;

        //Details of the two teams in the match
        string home_club_name;
        string away_club_name;
        int home_club_id;
        int away_club_id;
        
        public Home()
        {
            InitializeComponent();            
        }        

        //This method is called when the user clicks the 'Exit' button
        private void HomeExitMenuItem_Click(object sender, EventArgs e)
        {
            //Close the application
            Application.Exit();
        }

        //This method is called when the user clicks on the 'New Match' button
        private void HomeNewMatchBtn_Click(object sender, EventArgs e)
        {
            //Disable the 'New Match' button to prevent the
            //user from clicking it again while the Match is created
            HomeNewMatchBtn.Enabled = false;

            //Connect to the database
            connectToDatabase();
            
            //Reset all the dummy data for the Match
            //within the database to its initial state
            string delete_cautions = @"DELETE FROM caution;";
            string delete_sendoffs = @"DELETE FROM sendoff;";
            string delete_goals = @"DELETE FROM goal;";
            string delete_subs = @"DELETE FROM sub;";
            string onpitch_players = @"UPDATE player SET on_pitch=1 WHERE reset_value=1;";
            string offpitch_players = @"UPDATE player SET on_pitch=0 WHERE reset_value=0;";
            
            //Clear all Caution data
            using (SqlCeCommand reset_cautions = new SqlCeCommand(delete_cautions, conn))
            {
                reset_cautions.ExecuteNonQuery();
            }

            //Clear all Sending Off data
            using (SqlCeCommand reset_sendoffs = new SqlCeCommand(delete_sendoffs, conn))
            {
                reset_sendoffs.ExecuteNonQuery();
            }

            //Clear all Goal data
            using (SqlCeCommand reset_goals = new SqlCeCommand(delete_goals, conn))
            {
                reset_goals.ExecuteNonQuery();
            }

            //Clear all Substitution data
            using (SqlCeCommand reset_subs = new SqlCeCommand(delete_subs, conn))
            {
                reset_subs.ExecuteNonQuery();
            }

            //Set up players who are on the pitch and on the subs bench at the start
            //of a new Match
            using (SqlCeCommand reset_onpitch = new SqlCeCommand(onpitch_players, conn))
            {
                reset_onpitch.ExecuteNonQuery();
            }
            using (SqlCeCommand reset_offpitch = new SqlCeCommand(offpitch_players, conn))
            {
                reset_offpitch.ExecuteNonQuery();
            }           

            //Now obtain all data for the new Match
            //Obtain details of the two clubs in the Match and
            //put the data in a DataSet
            string command = @"SELECT name, club_id FROM club"; 
            SqlCeDataAdapter da = new SqlCeDataAdapter(command, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "club_names");
            DataTable dt = ds.Tables["club_names"];
            
            //Set variables relating to the two teams in the Match
            home_club_name = (string)ds.Tables[0].Rows[0]["name"];
            away_club_name = (string)ds.Tables[0].Rows[1]["name"];
            home_club_id = (int)ds.Tables[0].Rows[0]["club_id"];
            away_club_id = (int)ds.Tables[0].Rows[1]["club_id"];

            //Create the new Match object, passing the id of the two teams
            //involved in the match allowing data to be configured within
            //the new Match
            Match theMatch = new Match(home_club_id, away_club_id);

            //Set the name of the home and away team on the relevant
            //buttons on the main Match screen
            theMatch.MatchHomeTeamBtn.Text = home_club_name;
            theMatch.MatchAwayTeamBtn.Text = away_club_name;
            
            //Find out if the new Match object has managed
            //to connect to the external GPS antenna
            if (theMatch.gpsFound)
            {
                //If the device is connected to the GPS antenna,
                //display the new Match screen to the user
                theMatch.Visible = true;
            }
            else
            {
                MessageBox.Show("GPS antenna not found");
            }

            //Disconnect from the database
            closeDatabaseConnection();

            //Enable the 'New Match' button again so the user can create
            //another match as required in the future
            HomeNewMatchBtn.Enabled = true;
        }

        //This method is called when the user clicks on the 'Delete Data' button
        private void HomeDeleteBtn_Click(object sender, EventArgs e)
        {
            //Confirm that the user wants to reset all the dummy data
            if ((MessageBox.Show("Are you sure you want to delete all data?", "Delete Data", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) == DialogResult.Yes)
            {
                //Connect to the database
                connectToDatabase();

                //Create SQL commands to delete data
                string delete_cautions = @"DELETE FROM caution;";
                string delete_sendoffs = @"DELETE FROM sendoff;";
                string delete_goals = @"DELETE FROM goal;";
                string delete_subs = @"DELETE FROM sub;";
                string onpitch_players = @"UPDATE player SET on_pitch=1 WHERE reset_value=1;";
                string offpitch_players = @"UPDATE player SET on_pitch=0 WHERE reset_value=0;";
                
                //Delete all cautions from database
                using (SqlCeCommand reset_cautions = new SqlCeCommand(delete_cautions, conn))
                {
                    reset_cautions.ExecuteNonQuery();
                }

                //Delete all sending offs from database
                using (SqlCeCommand reset_sendoffs = new SqlCeCommand(delete_sendoffs, conn))
                {
                    reset_sendoffs.ExecuteNonQuery();
                }

                //Delete all goals from database
                using (SqlCeCommand reset_goals = new SqlCeCommand(delete_goals, conn))
                {
                    reset_goals.ExecuteNonQuery();
                }

                //Delete all substitutions from database
                using (SqlCeCommand reset_subs = new SqlCeCommand(delete_subs, conn))
                {
                    reset_subs.ExecuteNonQuery();
                }

                //Reset players on pitch and on subs bench at the start of the Match
                using (SqlCeCommand reset_onpitch = new SqlCeCommand(onpitch_players, conn))
                {
                    reset_onpitch.ExecuteNonQuery();
                }
                using (SqlCeCommand reset_offpitch = new SqlCeCommand(offpitch_players, conn))
                {
                    reset_offpitch.ExecuteNonQuery();
                }

                //Disconnect from the database
                closeDatabaseConnection();
                
                //Inform the user that all the dummy data has been reset
                MessageBox.Show("Data Reset", "Reset Data", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
            }
        }

        //This method is used to connect to the database
        private void connectToDatabase()
        {
            // Retrieve the connection string
            conString = @"Data Source=\Program Files\RefOffPDA\RefOffPDA_DB.sdf";

            // Open the connection using the connection string
            conn = new SqlCeConnection(conString);
            conn.Open();
        }

        //This method is used to disconnect from the database
        private void closeDatabaseConnection()
        {
            //Close the database connection
            conn.Close();
        }                
    }
}